package com.soento.sample.web.controller;

import com.soento.core.base.lang.Resp;
import com.soento.core.base.util.HttpUtil;
import com.soento.core.base.util.JsonUtil;
import com.soento.web.support.BaseController;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Map;

@RestController
@RequestMapping("http")
public class HttpTestController extends BaseController {

    @GetMapping("get")
    public Resp get() {
        String url = "http://127.0.0.1:8080/testGet?aaa=1&bbb=2";
        String rtn = HttpUtil.get(url);
        return success(rtn);
    }

    @PostMapping("post")
    public Resp post() {
        String url = "http://127.0.0.1:8080/testPost";
        String params = "aaa=1&bbb=2";
        String rtn = HttpUtil.post(url, params);
        return success(rtn);
    }

    @RequestMapping(value = "postJson", method = RequestMethod.GET)
    @ResponseBody
    public Resp postJson() {
        String url = "http://127.0.0.1:8080/testPostJson";
        Map<String, String> params = new HashedMap();
        params.put("a", "1");
        params.put("b", "2");
        String rtn = HttpUtil.postJson(url, JsonUtil.toJson(params));
        return success(rtn);
    }

    @RequestMapping(value = "upload", method = RequestMethod.GET)
    @ResponseBody
    public Resp upload() {
        String url = "http://127.0.0.1:8080/upload";
        String params = "aaa=1&bbb=2";
        File[] files = new File[2];
        files[0] = new File("/app/色情.jpg");
        files[1] = new File("/app/text.txt");
        String rtn = HttpUtil.upload(url, files, params);
        return success(rtn);
    }

    @RequestMapping(value = "download", method = RequestMethod.GET)
    @ResponseBody
    public Resp download() {
        String url = "http://127.0.0.1:8080/色情.jpg";
        String path = "/app/色情1.jpg";
        File f = HttpUtil.download(url, path);
        return success(f.getPath());
    }
}
